﻿

namespace LxCFD
{
    public class SolveCenter : IReset
    {
        public bool IsTransient;
        public TransientTimeBase TransientTime;
        public InitializationGroup InitializeGroup = new InitializationGroup();
        public DistanceGroup WallDistanceGroup = new DistanceGroup();
        public TypicalEquationGroup TemperatureGroup = new TypicalEquationGroup();
        public SIMPLEGroup SIMPLEGroup = new SIMPLEGroup();
        public TurbulenceGroup TurbulenceGroup = new TurbulenceGroup();
        public void Reset(ResetTypes resetType)
        {
            if (resetType == ResetTypes.None) return;
            IsTransient = false;
            TransientTime = null;
            InitializeGroup.Clear();
            WallDistanceGroup.Clear();//实际上下一次计算自动更新掉就的，但是，如果导入新网格，则需要清理内存
            TemperatureGroup.Clear();
            SIMPLEGroup.Clear();
            TurbulenceGroup.Clear();
        }
        public void InitializeFields(bool isForce)
        {
            if (IsTransient)
            {
                AppRes.SolveCenter.TransientTime.Initialize();
            }
            //重新分组
            ConnectivityAnalysis ca = new ConnectivityAnalysis();
            ca.LoadMeshInf(AppRes.FvMesh);

            WallDistanceGroup.Rearrange(ca);
            TemperatureGroup.Rearrange(ca);
            SIMPLEGroup.Rearrange(ca);
            TurbulenceGroup.Rearrange(ca);
            //初始化场
            foreach (var g in AppRes.SolveCenter.WallDistanceGroup.Units)
            {
                g.Initialize(isForce);
            }
            InitializeGroup.Initialize(isForce);
        }
        public void Solve_Steady()
        {
            MultiThreads.RunCalTasks(TurbulenceGroup.UpdateGammaForMomentum);
            SIMPLEGroup.Solve_Steady();
            TurbulenceGroup.Solve_Steady();
            MultiThreads.RunCalTasks(TurbulenceGroup.UpdateGammaForTemperature);
            TemperatureGroup.Solve_Steady();
        }
        public void Solve_TransientImplicit()
        {
            MultiThreads.RunCalTasks(TurbulenceGroup.UpdateGammaForMomentum);
            SIMPLEGroup.Solve_ImplicitTransient();
            TurbulenceGroup.Solve_ImplicitTransient();
            MultiThreads.RunCalTasks(TurbulenceGroup.UpdateGammaForTemperature);
            TemperatureGroup.Solve_ImplicitTransient();
        }
        public void Solve_TransientExplicit()
        {
            TemperatureGroup.Solve_ExplicitTransient();
        }
        public void SaveOldValue()
        {
            TemperatureGroup.SaveOldValue();
        }


    }
}
